package com.printeverywhere.delegator;

import static com.printeverywhere.util.AdminConstants.GET_LIST_WITH_SEARCH_LIST_CRITERIA;
import static com.printeverywhere.util.AdminConstants.SESSION_CLOSED;
import static com.printeverywhere.util.AttributeConstants.CLOSING_SESSION;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.printeverywhere.dto.GeneralCategories;
import com.printeverywhere.dto.Items;
import com.printeverywhere.dto.ItemsWatchlist;
import com.printeverywhere.util.HibernateUtil;
import com.printeverywhere.util.SearchListCriteria;

public class ItemsWatchListServiceDelegator {
	private static Logger logger = Logger.getLogger(ItemsWatchListServiceDelegator.class);
	/**
	 * Gets the list.
	 *
	 * @return the list
	 *
	 * @throws HibernateException the hibernate exception
	 */
	@SuppressWarnings("unchecked")
	public static List<ItemsWatchlist> getList() throws HibernateException {
		if(logger.isInfoEnabled()){
			logger.info("Getting list for Bank Account without search criteria".intern());
		}
		Session session = HibernateUtil.getSession();
		try {
			Transaction transaction = session.beginTransaction();
			List<ItemsWatchlist> list = session.createCriteria(ItemsWatchlist.class).list();
			transaction.commit();
			return list;
		} catch (HibernateException e) {
			logger.error("Got exception when getting list for Bank Account without search criteria".intern());
			throw e;
		} finally {
			if(logger.isInfoEnabled()){
				logger.info(CLOSING_SESSION);
			}
			session.flush();
			session.close();
		}
	}

	/**
	 * Returns the list of the ItemsWatchlist based on the conditions
	 * specified in searchListCriteria. It checks for the given values equality,
	 * non-equality, greater, less, like, ...etc. operations.
	 *
	 * @param searchCriteria
	 *            the search criteria
	 *
	 * @return the list
	 *
	 * @throws HibernateException
	 *             the hibernate exception
	 */
	public static List<ItemsWatchlist> getList(
			SearchListCriteria searchCriteria) throws HibernateException {
		logger.info(GET_LIST_WITH_SEARCH_LIST_CRITERIA);
		Session session = HibernateUtil.getSession();
		Criteria criteria = session.createCriteria(ItemsWatchlist.class);
		String orderBY = searchCriteria.getOrderBy();
		if (orderBY != null) {
			if (searchCriteria.isAsc()) {
				criteria.addOrder(Order.asc(orderBY));
			} else {
				criteria.addOrder(Order.desc(orderBY));
			}
		}
		Set<String> keys = searchCriteria.getAttributeNames();
		Iterator<String> it = keys.iterator();
		String key = null;
		Object[] value = null;
		while (it.hasNext()) {
			key = it.next();
			value = searchCriteria.getAttribute(key);
			if (((String) value[1]).equals(SearchListCriteria.EQ)) {
				criteria.add(Restrictions.eq(key, value[0]));
			} else if (((String) value[1]).equals(SearchListCriteria.GE)) {
				criteria.add(Restrictions.ge(key, value[0]));
			} else if (((String) value[1]).equals(SearchListCriteria.GT)) {
				criteria.add(Restrictions.gt(key, value[0]));
			} else if (((String) value[1]).equals(SearchListCriteria.ILIKE)) {
				criteria.add(Restrictions.ilike(key, value[0]));
			} else if (((String) value[1]).equals(SearchListCriteria.ISNOTNULL)) {
				criteria.add(Restrictions.isNotNull(key));
			} else if (((String) value[1]).equals(SearchListCriteria.ISNULL)) {
				criteria.add(Restrictions.isNull(key));
			} else if (((String) value[1]).equals(SearchListCriteria.LE)) {
				criteria.add(Restrictions.le(key, value[0]));
			} else if (((String) value[1]).equals(SearchListCriteria.LIKE)) {
				criteria.add(Restrictions.like(key, value[0]));
			} else if (((String) value[1]).equals(SearchListCriteria.LT)) {
				criteria.add(Restrictions.lt(key, value[0]));
			} else if (((String) value[1]).equals(SearchListCriteria.NE)) {
				criteria.add(Restrictions.ne(key, value[0]));
			} else if (((String) value[1]).equals(SearchListCriteria.IN)) {
				criteria.add(Restrictions.in(key, (Object[]) value[0]));
			}
		}
		Transaction transaction = session.beginTransaction();
		List<ItemsWatchlist> list = new ArrayList<ItemsWatchlist>();
		try {
			list = criteria.list();
			transaction.commit();
		} catch (HibernateException e) {
			for (String keyexception : keys) {
				logger.info(keys);
			}
			throw e;
		} finally {
			logger.info(SESSION_CLOSED);
			session.flush();
			HibernateUtil.closeSession();
		}
		return list;
	}
}
